All files / src/providers ErrorProvider.tsx

0% Statements 0/109
0% Branches 0/52
0% Functions 0/27
0% Lines 0/109

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client';
 
import React, { createContext, useContext, useCallback, useEffect } from 'react';
import { Toaster } from 'sonner';
import { useQueryClient } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';
import { toast } from 'sonner';
import { useTranslation } from 'react-i18next';
import i18n from 'i18next';
import { authCleanup } from '@/utils/authCleanup';
 
interface ApiError {
  response?: {
    status: number;
    data?: {
      error?: string;
      message?: string;
      details?: string;
    };
  };
  message?: string;
  code?: string;
}
 
interface ErrorContextType {
  reportError: (error: Error, context?: string) => void;
  handleApiError: (error: ApiError | Error | unknown, context?: string) => void;
  handleAuthError: () => void;
  handleNetworkError: () => void;
  clearErrors: () => void;
}
 
const ErrorContext = createContext<ErrorContextType | undefined>(undefined);
 
interface ErrorProviderProps {
  children: React.ReactNode;
}
 
export function ErrorProvider({ children }: ErrorProviderProps) {
  const queryClient = useQueryClient();
  const router = useRouter();
  const { t } = useTranslation();
 
  // Global error reporting function
  const reportError = useCallback((error: Error, context?: string) => {
    console.group('🚨 Global Error Report');
    console.error('Context:', context || 'Unknown');
    console.error('Error:', error);
    console.error('Stack:', error.stack);
    console.error('Timestamp:', new Date().toISOString());
    console.groupEnd();
 
    // In production, send to error monitoring service
    // Example: Sentry.captureException(error, { tags: { context } });
  }, []);
 
  // Handle authentication errors
  const handleAuthError = useCallback(() => {
    // Clear all cached data
    queryClient.clear();
    
    // Clear auth-related data consistently
    authCleanup.clearAllAuthData();
    
    // Show logout message
    toast.error(t('common.sessionExpiredTitle'), {
      description: t('common.sessionExpiredDescription'),
      duration: 4000});
 
    // Redirect to login
    setTimeout(() => {
      router.push('/login');
    }, 1000);
  }, [queryClient, router]);
 
  // Handle API errors with specific logic
  const handleApiError = useCallback((error: ApiError | Error | unknown, context?: string) => {
    let errorMessage = t('common.somethingWentWrong');
    let shouldLogout = false;
    let shouldRetry = false;
 
    // Parse error response (safely)
    const maybeResp = (error as any)?.response;
    if (maybeResp) {
      const status = typeof maybeResp.status === 'number' ? maybeResp.status : undefined;
      const data = maybeResp.data as any;
 
      if (typeof status === 'number') {
        switch (status) {
          case 401:
            errorMessage = t('common.sessionExpiredDescription');
            shouldLogout = true;
            break;
          case 403:
            errorMessage = t('common.noPermission');
            break;
          case 404:
            errorMessage = t('common.notFound');
            break;
          case 422:
            errorMessage = data?.error?.details || t('common.invalidData');
            break;
          case 429:
            errorMessage = t('common.tooManyRequests');
            shouldRetry = true;
            break;
          case 500:
            errorMessage = t('common.serverError');
            shouldRetry = true;
            break;
          case 503:
            errorMessage = t('common.serviceUnavailable');
            shouldRetry = true;
            break;
          default:
            errorMessage = data?.error?.details || data?.message || errorMessage;
        }
      }
    } else if ((error as any)?.message) {
      errorMessage = String((error as any).message);
    }
 
    // Show appropriate toast
    toast.error(context ? `${context} ${t('common.failed')}` : t('common.error'), {
      description: errorMessage,
      duration: shouldRetry ? 6000 : 5000,
      action: shouldRetry ? {
        label: t('common.tryAgain'),
        onClick: () => {
          // Retry logic would be handled by the calling component
          toast.info(t('common.tryAgain'));
        }} : undefined});
 
    // Handle logout if needed
    if (shouldLogout) {
      setTimeout(() => {
        handleAuthError();
      }, 2000);
    }
 
    // Report error
    reportError(new Error(errorMessage), context);
  }, [reportError, handleAuthError]);
 
  // Handle network errors
  const handleNetworkError = useCallback(() => {
    toast.error(t('common.networkErrorTitle'), {
      description: t('common.networkErrorDescription'),
      duration: 6000,
      action: {
        label: t('common.tryAgain'),
        onClick: () => {
          window.location.reload();
        }}});
  }, []);
 
  // Clear all errors
  const clearErrors = useCallback(() => {
    toast.dismiss();
  }, []);
 
  // Global error handler for unhandled promise rejections
  useEffect(() => {
    const handleUnhandledRejection = (event: PromiseRejectionEvent) => {
      console.error('Unhandled promise rejection:', event.reason);
      reportError(
        new Error(event.reason?.message || 'Unhandled promise rejection'),
        'Unhandled Promise'
      );
      
      // Prevent the default browser behavior
      event.preventDefault();
    };
 
    const handleError = (event: ErrorEvent) => {
      console.error('Global error:', event.error);
      reportError(
        event.error || new Error(event.message),
        'Global Error'
      );
    };
 
    window.addEventListener('unhandledrejection', handleUnhandledRejection);
    window.addEventListener('error', handleError);
 
    return () => {
      window.removeEventListener('unhandledrejection', handleUnhandledRejection);
      window.removeEventListener('error', handleError);
    };
  }, [reportError]);
 
  // React Query global error handler
  useEffect(() => {
    queryClient.setDefaultOptions({
      queries: {
        retry: (failureCount, error: ApiError | Error | unknown) => {
          // Don't retry on 4xx errors (client errors) - check status safely
          const status = (error as any)?.response?.status;
          if (typeof status === 'number' && status >= 400 && status < 500) {
            return false;
          }
          // Retry up to 3 times for other errors
          return failureCount < 3;
        },
        retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
        // onError is deprecated in newer versions of React Query
        // Error handling is now done in individual queries or mutations
      },
      mutations: {
        // onError is deprecated in newer versions of React Query
        // Error handling is now done in individual mutations
      }});
  }, [queryClient, handleAuthError, handleNetworkError]);
 
  const value: ErrorContextType = {
    reportError,
    handleApiError,
    handleAuthError,
    handleNetworkError,
    clearErrors};
 
  return (
    <ErrorContext.Provider value={value}>
      {children}
      <Toaster
        position="top-right"
        expand={true}
        richColors={true}
        closeButton={true}
        toastOptions={{
          duration: 4000,
          style: {
            background: 'white',
            border: '1px solid #e5e7eb',
            color: '#374151'},
          className: 'toast'}}
      />
    </ErrorContext.Provider>
  );
}
 
export function useError() {
  const context = useContext(ErrorContext);
  if (context === undefined) {
    throw new Error('useError must be used within an ErrorProvider');
  }
  return context;
}
 
// Higher-order component to wrap components with error handling
export function withErrorHandling<P extends object>(
  Component: React.ComponentType<P>,
  errorFallback?: React.ComponentType<{ error: Error; retry: () => void }>
) {
  return function WrappedComponent(props: P) {
    const { reportError } = useError();
 
    const handleError = useCallback((error: Error, _errorInfo: React.ErrorInfo) => {
      reportError(error, `Component: ${Component.displayName || Component.name}`);
    }, [reportError]);
 
    return (
      <ErrorBoundary onError={handleError} fallback={errorFallback}>
        <Component {...props} />
      </ErrorBoundary>
    );
  };
}
 
 // Simple error boundary for the HOC
 type ErrorBoundaryProps = {
   children: React.ReactNode;
   onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
   fallback?: React.ComponentType<{ error: Error; retry: () => void }>;
 };
 
 class ErrorBoundary extends React.Component<
   ErrorBoundaryProps,
   { hasError: boolean; error: Error | null }
 > {
   constructor(props: ErrorBoundaryProps) {
    super(props);
    this.state = { hasError: false, error: null };
  }
 
  static getDerivedStateFromError(error: Error) {
    return { hasError: true, error };
  }
 
  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    if (this.props.onError) {
      this.props.onError(error, errorInfo);
    }
  }
 
  render() {
    if (this.state.hasError && this.state.error) {
      if (this.props.fallback) {
        const FallbackComponent = this.props.fallback;
        return (
          <FallbackComponent
            error={this.state.error}
            retry={() => this.setState({ hasError: false, error: null })}
          />
        );
      }
 
      return (
        <div className="p-4 border border-red-200 rounded-lg bg-red-50">
          <h3 className="text-red-800 font-medium">{i18n.t('common.somethingWentWrong')}</h3>
          <p className="text-red-600 text-sm mt-1">{this.state.error.message}</p>
          <button
            onClick={() => this.setState({ hasError: false, error: null })}
            className="mt-2 px-3 py-1 bg-red-600 text-white text-sm rounded hover:bg-red-700"
          >
            {i18n.t('common.tryAgain')}
          </button>
        </div>
      );
    }
 
    return this.props.children;
  }
}